package com.cenwangkeji.resident.ui.activity;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.MessageBean;
import com.cenwangkeji.resident.model.bean.MessageHistoryBean;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.cenwangkeji.resident.model.bean.User;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class MessageDetailActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_content)
    TextView mTvContent;
    @BindView(R.id.tv_service)
    TextView mTvService;
    @BindView(R.id.btn_cancel)
    Button mBtnCancel;
    @BindView(R.id.btn_confirm)
    Button mBtnConfirm;
    private MessageBean mMessageBean;
    private int mId;

    @Override
    public int setLayoutId() {
        return R.layout.activity_message_detail;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        String json = getIntent().getStringExtra("date");
        if (!TextUtils.isEmpty(json)) {
            MyLogger.i(json);
            Gson gson = new Gson();
            mMessageBean = gson.fromJson(json, MessageBean.class);
            mTvTitle.setText("姓名：" + mMessageBean.getGrName() + "       时间：" + mMessageBean.getDate());
            mTvService.setText("随访项目：" + mMessageBean.getZxbName());
            mId= mMessageBean.getSfId();
            mTvContent.setText(mMessageBean.getContent());
        } else {
            MessageHistoryBean.DataBean.RowsBean bean = getIntent().getParcelableExtra("msg");
            mTvTitle.setVisibility(View.INVISIBLE);
            mTvService.setVisibility(View.INVISIBLE);
            mTvContent.setText(bean.getMsg());
            if (bean.getStatus()==1){
                mBtnCancel.setVisibility(View.INVISIBLE);
                mBtnConfirm.setVisibility(View.INVISIBLE);
            }
            mId = bean.getSfId();

        }


    }


    @OnClick({R.id.btn_cancel, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                new MaterialDialog.Builder(this)
                        .title("延期几天")
                        .content("")
                        .inputType(InputType.TYPE_CLASS_NUMBER)
                        .input("填写数字", null, new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(MaterialDialog dialog, CharSequence input) {
                                if (TextUtils.isEmpty(input)) {
                                    MyToast.show("请输入延期天数");
                                } else {
                                    getData(false, Integer.valueOf(input + ""));
                                }
                            }
                        })
                        .positiveText("确定")
                        .show();
                break;
            case R.id.btn_confirm:
                showBackDialog();
                break;
        }
    }


    private void showBackDialog() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("确定");
        normalDialog.setMessage("是否确定");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        getData(true, 0);

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }

    private void getData(final boolean is, final int num) {
        Observable.create(new ObservableOnSubscribe<SuccessBean>() {
            @Override
            public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                Map<Object, Object> map = new HashMap<>();
                map.put("mark", 3);
                map.put("id", mId);
                map.put("token", User.getToken());
                map.put("flag", is);
                map.put("num", num);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.message_result);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, SuccessBean.class));

            }
        }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
            @Override
            public void onNext(SuccessBean bean) {
                if (bean.getCode() == 0) {
                    MyToast.show("确认成功");
                    finish();
                } else {
                    MyToast.show(bean.getMsg());
                }
            }
        });
    }


}
